import platform
import os
import time
import threading
import socket
 
live_ip = 0
 
 
def get_os():
    os = platform.system()
    if os == "Windows":
        return "n"
    else:
        return "c"
 
 
def ping_ip(ip_str):
    cmd = ["ping", "-{op}".format(op=get_os()),
           "1", ip_str]
    output = os.popen(" ".join(cmd)).readlines()
    for line in output:
        if str(line).upper().find("TTL") >= 0:
            return False
    return True
 
 
def find_ip(ip_prefix):
    """
    给出当前的ip地址段 ，然后扫描整个段所有地址
    """
    threads = []
    for i in range(10, 256):
        ip = '%s.%s' % (ip_prefix, i)
        # threads.append(threading.Thread(target=ping_ip, args={ip, }))
        if ping_ip(ip):
            return ip
    return "0.0.0.0" # no free ip
 
 
def find_local_ip():
    """
    获取本机当前ip地址
    :return: 返回本机ip地址
    """
    myname = socket.getfqdn(socket.gethostname())
    myaddr = socket.gethostbyname(myname)
    return myaddr

def get_free_ip(ip_pre):
    return find_ip(ip_pre)
 
 
if __name__ == "__main__":
    IP_PRE = "10.112.43"
    print(get_free_ip(IP_PRE))
